//
//  APIMycityGetTotalRankData.m
//  Bracelet
//
//  Created by jing zhao on 2/27/15.
//  Copyright (c) 2015 jing zhao. All rights reserved.
//

#import "APIMycityGetTotalRankData.h"

#import "APIConnection.h"

#import <AFNetworking/AFNetworking.h>

@implementation APIOneCityTotalRank

-(instancetype) initFromDictionaryPaper:(NSDictionary*)dic
{
    self = [super init];
    if (self) {
        self.strCityId          = [dic objectForKey:APIKEYCITYID];
        self.strCityName        = [dic objectForKey:APIKEYCITYNAME];
        self.strCityRunRanking  = [dic objectForKey:APIKEYCITYRUNRANKING];
        self.strCityOkproportion    = [dic objectForKey:APIKEYOKPROPORTION];
        self.strCityRunTotal_metre  = [dic objectForKey:APIKEYRUNTOTAL_METRE];
        self.strCityMax_metre       = [dic objectForKey:APIKEYMAXMETRE];
        self.strCityTotal_money     = [dic objectForKey:APIKEYTOTAL_MONEY];
        self.strCityRankNum         = [dic objectForKey:APIKEYRANKNUM];
    }
    return self;
}

+(instancetype)apiFromDictionaryPaper:(NSDictionary*)dic
{
   return  [[APIOneCityTotalRank alloc]initFromDictionaryPaper:dic];
}
@end

@implementation APIMycityGetTotalRankData


-(instancetype) initFromRankData:(NSDictionary*)dic
{
    self = [super init];
    if (self) {
        NSDictionary *dicMyCityData = [dic objectForKey:APIKEYMYCITYDATA];
        NSArray *arrayRank = [dicMyCityData objectForKey:APIKEYCITYRANINGDATALIST];
        NSDictionary *myRank = [dicMyCityData objectForKey:APIKEYTHISCITYDATA];
        NSDictionary *paper = [dic objectForKey:APIKEYPAPER];
        
      self.paper = [APIPaper apiFromDictionaryPaper:paper];
      self.myRank = [APIOneCityTotalRank apiFromDictionaryPaper:myRank];
        _arrayAllRank = [[NSMutableArray alloc]init];
        for (NSDictionary *dic in arrayRank) {
            APIOneCityTotalRank *oneRank = [APIOneCityTotalRank apiFromDictionaryPaper:dic];
            [_arrayAllRank addObject:oneRank];
        }

    }
    return self;
}

+(instancetype)apiRankWithData:(NSDictionary*)dic
{
    return  [[self alloc]initFromRankData:dic];
}

+(void)getCityRankOfCityWithPagePaper:(NSString*)page
                      getTheTotalRank:(void (^)(APIMycityGetTotalRankData *outRank))successBlock
                              failure:(void (^)( NSError *error)) failureBlock
{
     NSString *strCityId = [[[NSUserDefaults standardUserDefaults]objectForKey:APIKEYUSERINFO]objectForKey:APIKEYCITYID];
    NSDictionary *data = @{@"flag":@"city",@"pager.pageNo":page,@"cityId":strCityId};
    
    [[APIConnection shareInstance]post:@"mycity/getTotalRankData_city" data:data netError:^(NSError *error) {
        failureBlock(error);
    } apiFinish:^(id data) {
        
        NSDictionary *dictionaryTemp = data;
        NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
        if (codeTemp ==0) {
            NSDictionary *dicSuccess = [dictionaryTemp objectForKey:APIKEYData];
            successBlock([APIMycityGetTotalRankData apiRankWithData:dicSuccess]);
        }
        else
        {
            failureBlock(nil);
        }
    }];
}
+(void)getCityRankOfWeekWithPagePaper:(NSString*)page
                      getTheTotalRank:(void (^)(APIMycityGetTotalRankData *outRank))successBlock
                              failure:(void (^)( NSError *error)) failureBlock
{
    NSString *strCityId = [[[NSUserDefaults standardUserDefaults]objectForKey:APIKEYUSERINFO]objectForKey:APIKEYCITYID];
    NSDictionary *data = @{@"flag":@"week",@"pager.pageNo":page,@"cityId":strCityId};
    
    [[APIConnection shareInstance]post:@"mycity/getTotalRankData_city" data:data netError:^(NSError *error) {
        failureBlock(error);
    } apiFinish:^(id data) {
        
        NSDictionary *dictionaryTemp = data;
        NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
        if (codeTemp ==0) {
            NSDictionary *dicSuccess = [dictionaryTemp objectForKey:APIKEYData];
            successBlock([APIMycityGetTotalRankData apiRankWithData:dicSuccess]);
        }
        else
        {
            failureBlock(nil);
        }
    }];

}
@end
